// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

 
import animUtil from './anim_util';
import particalUtil from './partical_util';
import UIController from './ui_controller';
import app_data from './app_data';
import constants from './constants';
import ParticalUtil from './partical_util';
import DropsGroup from './drops_group';
import BallGroup from './ball_group';


@ccclass
export default class MainScene extends cc.Component {

    defaultRandomBallCount=8;
    defaultRestBallCount=1;


    @property(cc.Node)
    joystick:  cc.Node=null;

    @property(cc.Prefab)
    ballPrefab:  cc.Prefab=null;

    @property(cc.Label)
    labelScore:  cc.Label=null;

    isFiringBall=false;

   


    @property(cc.Integer)
    randomBallCount:  number=this.defaultRandomBallCount;

    _score:  number=0;

    private static _inst:MainScene;

    static getInst():MainScene
    {
        return this._inst;
    }

    get score () {
        return this._score;
    }

    set score (value) {
      
    }

    @property(cc.Label)
    labelRestBallCount:cc.Label=null
    
    _restBallCount:number=this.defaultRestBallCount;

    get restBallCount () {
        return this._restBallCount;
    }

    set restBallCount (value) {
        this._restBallCount=value;
        this.labelRestBallCount.string="x"+value;
    }

    @property(cc.Prefab)
    particalBallBombPrefab:cc.Prefab=null;

  

    ballNode:cc.Node=null;

    ballActionSmallToBig=null;

  

    isTouchStart:boolean=false;

    pressTime:number=0;

    checkGameOverLoop;

    @property({type:DropsGroup})
    dropsGroup:DropsGroup=null;

    @property({type:BallGroup})
    ballGroup:BallGroup=null;

   

    


    // LIFE-CYCLE CALLBACKS:

    // use this for initialization
    onLoad () {
        MainScene._inst=this;
        this.setEventControl();
        cc.director.on(constants.GAME_EVENT.BallMerge,(e:any)=>{
 
            this.restBallCount+=1;
            var partical= ParticalUtil.getInst().addParticle(this.particalBallBombPrefab,this.node,e.detail.mergePos,false);
            
              partical.resetSystem();
        });
        ParticalUtil.getInst().addParticle(this.particalBallBombPrefab,this.node,cc.v2(-1000,-1000),false).resetSystem();
        this.joystick.active=true;
    }
    start(){
        let first_game=localStorage.getItem(constants.LOCAL_STORAGE_ITEM.FirstGame);
        // console.log(first_game)
        // if(!first_game){
         
            localStorage.setItem(constants.LOCAL_STORAGE_ITEM.FirstGame,String(true));
            UIController.getInst().showWithMask(UIController.UiPrefabPath.Teach);
        // }
        this.startGame();
    }

    // called every frame
    // update (dt) {

    // }
    resetGame(){
        this.score=0;
        // this.gameOverPanel.active=false;
       
         
        this.restBallCount=this.defaultRestBallCount;
        this.randomBallCount=this.defaultRandomBallCount;
         this.startGame();
    }
    startGame(){
        cc.director.getPhysicsManager().gravity = cc.v2(0,-10);
        this.dropsGroup.startAction();
        this.ballGroup.init(this.joystick.parent.parent.convertToNodeSpaceAR(this.joystick.convertToWorldSpaceAR(cc.v2(0,0))));
    }
  
    setEventControl(){
        this.node.on(cc.Node.EventType.TOUCH_START,( event )=> {
            if(this.isFiringBall===true){
                return;
            }
            this.pressTime=new Date().getTime();
            this.isFiringBall=true;
            // this.joystick.getComponent("joystick").reset();
            this.ballNode= this.ballGroup.getNewBalls();
            
         });
        // this.node.on(cc.Node.EventType.TOUCH_MOVE,( event:any )=> {
         
        // });
        this.node.on(cc.Node.EventType.TOUCH_CANCEL,( event )=> {
           
                this.fireBall(event);
         });
        this.node.on(cc.Node.EventType.TOUCH_END,( event )=> {
                this.fireBall(event);

        });
    }
  
    fireBall(event){
        let pressTimeDt=new Date().getTime()-this.pressTime;
        var currentP = cc.p(this.joystick.parent.convertToWorldSpaceAR(this.joystick.getPosition()));
        var deltaP = currentP.sub(event.getLocation());
        var angle = cc.pToAngle(deltaP) / Math.PI * 180;
     
       if(-angle<30&&-angle>-90){
            angle=-30;
        }
        else if(-angle>150||(-angle<-90)){
            angle=-150;
        }
        angle=-angle-90;
      
      
         this.joystick.rotation =angle;
      
       
        this.ballNode.getComponent("ball").fire(this.joystick.rotation,pressTimeDt);
        this.isFiringBall=false;
     
    }
    checkGameOver(){
      
    }
    gameOver(){
        UIController.getInst().showWithMask(UIController.UiPrefabPath.GameOver);
        
    }

  
    // update (dt) {}
}
